package com.zsw.customer1.service.impl;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zsw.customer1.service.testServiceProducer2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;


/**
 * 使用 rabbion+restTemlate 微服务之间的通信
 * 使用 Hystrix  熔断降级服务
 */
@Service
public class testServiceProducer2Impl implements testServiceProducer2 {


    @Autowired
    RestTemplate restTemplate;


    @Override
    @HystrixCommand(fallbackMethod = "hiHystrix")
    public String hi(String id) {
        String message = restTemplate.getForObject("http://serviceproducer2/id" + id, String.class);
        return message;
    }

    @Override
    @HystrixCommand(fallbackMethod = "testHystrix")//这是回退方法的名称，回退方法只能系在同一个类里
    public String test() {

        String message = restTemplate.getForObject("http://serviceproducer2/client", String.class);
        return message;
    }

    private String message = "当前调用的服务端是ServiceProducer2，该服务端异常";

    public String testHystrix(Throwable throwable) {
        System.out.println("rabbion服务调用ServiceProducer2不可用，降级处理，method:test，异常：" + throwable);
        return message;
    }

    public String hiHystrix(String id, Throwable throwable) {
        System.out.println("rabbion服务调用ServiceProducer2不可用，降级处理，method:hi，参数：[" + id + "],异常：" + throwable);
        return message;
    }
}
